<template>
	<view class="body">
		<view>
			<h2 class="title">通知预警</h2>
			<u-search shape="square" :show-action="false"></u-search>
		</view>
		<view style="margin: 20vh auto;" v-if="showList.length==0">
			<u-empty text="暂时没有预警信息" mode="list"></u-empty>
		</view>
		<view class="card" v-for="item in showList" @click="toWarnDetail(item)">
			<view class="tag">
				<u-tag :text="item.warn.warningdown=='1'?'灌溉':'排水'" :type="item.warn.warningdown=='1'?'warning':'success'" mode="dark" />
			</view>
			<view class="img">
				<image :src="'https://crop.lxylw.design'+item.farm.soilphotourl" mode=""></image>
			</view>
			<view class="content flex-ja">
				<view class="text flex-ac">{{item.warn.warninginfo}}!</view>
				<view>
					<u-circle-progress type="warning" :percent="item.farm.curwater" :width="160" :border-width="10"
						duration="2000">
						<view class="u-progress-content">
							<text class='u-progress-info'>{{item.farm.curwater}}%</text>
						</view>
					</u-circle-progress>
					<view style="text-align: center;">当前含水量</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getWarnByUserId
	} from "@/common/api/warn.js"
	import {
		getUser
	} from '@/common/entity/user.js'
	import {
		getFarm,
		setFarm
	} from '@/common/entity/farm.js'
	import {
		getFarmList
	} from "@/common/api/water.js"
	export default {
		data() {
			return {
				isShow1: true,
				isShow2: true,
				warnList: [],
				user: {},
				farm: {},
				showList: []
			}
		},
		beforeMount() {
			this.user = getUser()
			this.farm = getFarm()
			// console.log(this.user.id)
			getWarnByUserId(this.user.id).then(res => {
				this.warnList = res[1].data.data
				this.init()
			})
		},
		onPullDownRefresh() {
			console.log('refresh');
			// 更新农场
			getFarmList().then(res => {
				let rs = res[1].data.data.results
				let farm = []
				let id = getUser().id
				for (let i = 0; i < rs.length; i++) {
					if (rs[i].soilbelong == id) {
						farm.push(rs[i])
					}
				}
				setFarm(farm)
				this.farm = getFarm()
				// 更新预警表
				getWarnByUserId(this.user.id).then(res => {
					this.warnList = res[1].data.data
					// 更新showList
					this.init()
				})
				this.$u.toast("数据加载成功")
				uni.stopPullDownRefresh();
			})
		},
		methods: {
			init() {
				this.showList = []
				for (let i in this.warnList) {
					// console.log(this.warnList[i])
					this.showList.push({
						farm: this.farm.filter(a => {
							return this.warnList[i].soilid == a.soilid
						})[0],
						warn: this.warnList[i]
					})

				}
				// console.log(this.showList)
			},
			toWarnDetail(item) {
				uni.navigateTo({
					url: '../warnDetail/warnDetail?item='+JSON.stringify(item)
				})
			}
		}
	}
</script>

<style lang="scss">
	.body {
		background-color: #FFFFF;
		min-height: 1260rpx;
		padding: 28rpx;
		box-sizing: border-box;

		h2.title {
			margin-bottom: 20rpx;
		}

	}


	.topBar {
		padding: 20rpx;
		width: 100%;
		text-align: center;
		height: 48rpx;
		background-color: #fff;

		.img {
			position: absolute;
			top: 20rpx;
			left: 20rpx;
		}

		.title {
			font-weight: bold;
			font-size: 36rpx;
		}
	}

	.card {
		box-sizing: border-box;
		margin: 30rpx 0;
		background-color: #FFFFFF;
		border-radius: 12rpx;
		.tag{
			position: absolute;
			z-index: 10;
			right: 30rpx;
		}

		.img {
			height: 320rpx;
			overflow: hidden;
			border-radius: 12rpx;
			// box-shadow: 0 10rpx 10rpx #ccc;

			image {
				width: 100%;
			}
		}

		.content {
			line-height: 1.5em;
			font-size: 32rpx;
			color: #666;
			box-shadow: 6rpx 10rpx 10rpx 4rpx #EEE;
			padding: 24rpx;
			border-radius: 12rpx;

			.text {
				font-size: 32rpx;
				line-height: 2rem;
				margin-right: 28rpx;
				overflow: hidden;
			}
		}

		.btn {
			float: right;
		}

		.contentBox {
			margin: 30rpx 0;

			.img {
				height: 120rpx;
				width: 18vw;
				overflow: hidden;
				margin-right: 4vw;

				image {
					height: 100%;
					width: 100%;
				}
			}

			.titleRed {
				color: #D7050B;
				font-weight: bold;
				font-size: 32rpx;
			}

			.detail {
				font-size: 30rpx;
				color: #9A9A9A;
				width: 54vw;
				line-height: 54rpx;
			}
		}
	}
</style>
